package com.sdust.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdust.constant.MessageConstant;
import com.sdust.context.BaseContext;
import com.sdust.mapper.ChatMapper;
import com.sdust.pojos.dto.UserAddFriendDto;
import com.sdust.pojos.dto.UserChatDto;
import com.sdust.pojos.dto.UserSendMessageDto;
import com.sdust.pojos.entity.ChatFriends;
import com.sdust.pojos.entity.ChatMessage;
import com.sdust.result.Result;
import com.sdust.service.ChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
@Slf4j
public class ChatServiceImpl extends ServiceImpl<ChatMapper, ChatMessage> implements ChatService {

    @Autowired
    private ChatMapper chatMapper;

    /**
     * 获取当前用户好友列表
     * @return
     */
    @Override
    public Result getFriendsList() {

        Integer userId = BaseContext.getCurrentId();
        List<ChatFriends> friendsList = chatMapper.getFriendList(userId);

        return Result.success(friendsList);
    }

    /**
     * 获取聊天记录
     * @return
     */
    @Override
    public Result getChatRecords(UserChatDto dto) {
        List<ChatMessage> chatRecords = chatMapper.getChatRecords(dto.getUserId(), dto.getTargetUserId());
        return Result.success(chatRecords);
    }

    /**
     * 发送消息
     * @param dto
     * @return
     */
    @Override
    public Result sendMessage(UserSendMessageDto dto) {
        ChatMessage message = ChatMessage.builder()
                .userId(dto.getUserId())
                .userName(dto.getUserName())
                .targetUserId(dto.getTargetUserId())
                .content(dto.getContent())
                .targetUserName(dto.getTargetUserName())
                .createTime(LocalDateTime.now())
                .build();
        log.info("发送消息：dto：{}，message：{}", dto, message);
        chatMapper.insert(message);

        //更新最新的消息
        chatMapper.updateLatestMessage(message);

        return Result.success(MessageConstant.SEND_MESSAGE_SUCCESS);
    }

    /**
     * 添加好友
     * @param dto
     * @return
     */
    @Override
    public Result addFriend(UserAddFriendDto dto) {


        //判断是否已经是好友
        ChatFriends friendById = chatMapper.getFriendById(dto.getUserId(), dto.getFriendId());
        if(friendById != null) {
            return Result.error(MessageConstant.FRIEND_EXISTED);
        }

        //添加好友
        dto.setCreateTime(LocalDateTime.now());
        chatMapper.addFriend(dto);
        if(dto.getUserId() == dto.getFriendId()) {
            return Result.success(MessageConstant.ADD_FRIEND_SUCCESS);
        }
        //添加好友要双方互为好友
        Integer userId = dto.getUserId();
        Integer friendId = dto.getFriendId();
        String userName = dto.getUserName();
        dto.setUserId(friendId);
        dto.setFriendId(userId);
        dto.setUserFriendName(userName);
        chatMapper.addFriend(dto);

        return Result.success(MessageConstant.ADD_FRIEND_SUCCESS);
    }

}
